BIN = ./bin
LIB = ./lib
OBJ = ./obj
SRC = ./src
INCLUDE = ./src

COMPILER = g++
AR = ar
CXX_OPTIONS = -O2 -gdwarf-2
LINK_OPTS =
DYNLINK_OPTS =

LINK_OPTS = rcs
#DYNLINK_OPTS = -dynamiclib -install_name @executable_path/Frameworks/libUnderware.dylib
DYNLINK_OPTS = -dynamiclib -install_name @executable_path/../lib/libUnderware.dylib

OBJS = $(OBJ)/Thread.o $(OBJ)/Service.o $(OBJ)/Connector.o $(OBJ)/SecuredService.o $(OBJ)/SecuredConnector.o $(OBJ)/TypesCompatibility.o
LIB_OBJS = $(OBJ)/TypesCompatibility.o $(OBJ)/Connector.o $(OBJ)/SecuredConnector.o $(OBJ)/Thread.o

.PHONY = all clean

all : $(LIB)/libUnderware.dylib $(LIB)/libUnderwareStatic.a $(BIN)/client $(BIN)/server $(BIN)/secured_client $(BIN)/secured_server

clean :
	rm -f $(OBJ)/*.o
	rm -f $(BIN)/client $(BIN)/server $(BIN)/secured_client $(BIN)/secured_server $(LIB)/libUnderware.dylib $(LIB)/libUnderwareStatic.a

#####################
#### EJECUTABLES  ###
#####################

$(BIN)/client : $(OBJ)/client.o $(LIB)/libUnderware.dylib
	$(COMPILER) $(CXX_OPTIONS) -o $(BIN)/client $(OBJ)/client.o -I$(INCLUDE)/ -L$(LIB)/ -lUnderware

$(BIN)/server : $(OBJ)/server.o $(OBJ)/Service.o
	$(COMPILER) $(CXX_OPTIONS) -o $(BIN)/server $(OBJ)/server.o $(OBJ)/Service.o -I$(INCLUDE)/ -L$(LIB)/ -lUnderware
	
$(BIN)/secured_client : $(OBJ)/secured_client.o $(LIB)/libUnderware.dylib
	$(COMPILER) $(CXX_OPTIONS) -o $(BIN)/secured_client $(OBJ)/secured_client.o -I$(INCLUDE)/ -L$(LIB)/ -lUnderware

$(BIN)/secured_server : $(OBJ)/secured_server.o $(OBJ)/SecuredService.o $(OBJ)/Service.o
	$(COMPILER) $(CXX_OPTIONS) -o $(BIN)/secured_server $(OBJ)/secured_server.o $(OBJ)/SecuredService.o $(OBJ)/Service.o -I$(INCLUDE)/ -L$(LIB)/ -lUnderware -lssl -lcrypto

#####################
#### BIBLIOTECAS  ###
#####################

$(LIB)/libUnderware.dylib : $(LIB_OBJS)
	$(COMPILER) $(DYNLINK_OPTS) -o $(LIB)/libUnderware.dylib $(LIB_OBJS) -lssl -lcrypto

$(LIB)/libUnderwareStatic.a : $(LIB_OBJS)
	$(AR) $(LINK_OPTS) $(LIB)/libUnderwareStatic.a $(LIB_OBJS)

#####################
####  OBJETOS     ###
#####################

$(OBJ)/client.o : $(SRC)/client.cpp
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/client.cpp -o $(OBJ)/client.o -I$(INCLUDE)/
	
$(OBJ)/secured_client.o : $(SRC)/secured_client.cpp
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/secured_client.cpp -o $(OBJ)/secured_client.o -I$(INCLUDE)/

$(OBJ)/server.o : $(SRC)/server.cpp
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/server.cpp -o $(OBJ)/server.o -I$(INCLUDE)/
	
$(OBJ)/secured_server.o : $(SRC)/secured_server.cpp
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/secured_server.cpp -o $(OBJ)/secured_server.o -I$(INCLUDE)/
	
$(OBJ)/Connector.o : $(SRC)/Connector.cpp $(INCLUDE)/Connector.h
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/Connector.cpp -o $(OBJ)/Connector.o -I$(INCLUDE)/

$(OBJ)/Service.o : $(SRC)/Service.cpp $(INCLUDE)/Service.h
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/Service.cpp -o $(OBJ)/Service.o -I$(INCLUDE)/
	
$(OBJ)/SecuredConnector.o : $(SRC)/SecuredConnector.cpp $(INCLUDE)/SecuredConnector.h
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/SecuredConnector.cpp -o $(OBJ)/SecuredConnector.o -I$(INCLUDE)/

$(OBJ)/SecuredService.o : $(SRC)/SecuredService.cpp $(INCLUDE)/SecuredService.h
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/SecuredService.cpp -o $(OBJ)/SecuredService.o -I$(INCLUDE)/
	
$(OBJ)/Thread.o : $(SRC)/Thread.cpp
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/Thread.cpp -o $(OBJ)/Thread.o -I$(INCLUDE)/

$(OBJ)/TypesCompatibility.o : $(SRC)/TypesCompatibility.cpp $(INCLUDE)/TypesCompatibility.h
	$(COMPILER) $(CXX_OPTIONS) -c $(SRC)/TypesCompatibility.cpp -o $(OBJ)/TypesCompatibility.o -I$(INCLUDE)/
